<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"inkgn.gitee.io","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="因为某些历史原因，直接引入的多个JS文件之间可能会产生互相污染（变量、函数等），为了解决这个问题，在ES标准中引入了模块化的概念。 在ES5中使用commonJS 的方式进行模块化">
<meta property="og:type" content="article">
<meta property="og:title" content="浅析ES6中的模块化">
<meta property="og:url" content="https://inkgn.gitee.io/2021/08/12/article8/index.html">
<meta property="og:site_name" content="静水深流">
<meta property="og:description" content="因为某些历史原因，直接引入的多个JS文件之间可能会产生互相污染（变量、函数等），为了解决这个问题，在ES标准中引入了模块化的概念。 在ES5中使用commonJS 的方式进行模块化">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-08-12T10:22:46.000Z">
<meta property="article:modified_time" content="2021-08-12T10:22:46.000Z">
<meta property="article:author" content="yinks">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://inkgn.gitee.io/2021/08/12/article8/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>浅析ES6中的模块化 | 静水深流</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">静水深流</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/categories" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-motto">

    <a href="/motto/motto" rel="section"><i class="fa fa-book fa-fw"></i>古训</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/me" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://inkgn.gitee.io/2021/08/12/article8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/user_info/face">
      <meta itemprop="name" content="yinks">
      <meta itemprop="description" content="长路漫漫，半程风雨，半程春">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="静水深流">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          浅析ES6中的模块化
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-08-12 18:22:46" itemprop="dateCreated datePublished" datetime="2021-08-12T18:22:46+08:00">2021-08-12</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/es6/" itemprop="url" rel="index"><span itemprop="name">es6</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>因为某些历史原因，直接引入的多个JS文件之间可能会产生互相污染（变量、函数等），为了解决这个问题，在ES标准中引入了模块化的概念。</p>
<h2 id="在ES5中使用commonJS-的方式进行模块化"><a href="#在ES5中使用commonJS-的方式进行模块化" class="headerlink" title="在ES5中使用commonJS 的方式进行模块化"></a>在ES5中使用commonJS 的方式进行模块化</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&lt;script src=<span class="string">&quot;XXX/AAA.js&quot;</span> type=<span class="string">&quot;module&quot;</span>&gt;&lt;/script&gt;</span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;XXX/BBB.js&quot;</span> <span class="attr">type</span>=<span class="string">&quot;module&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p><code>type=&quot;module&quot;</code>表示使用模块化，在这种方式下，以上述代码为例，则由于模块作用域（每个模块文件都是独立的作用域，不是全局的），BBB中不能使用AAA文件中定义的东西，不加<code>type=&quot;module&quot;</code>则都会变成全局的，别人就能直接访问了。</p>
<h2 id="ES6中使用新的方法进行模块化：-使用-export-和-import-关键字"><a href="#ES6中使用新的方法进行模块化：-使用-export-和-import-关键字" class="headerlink" title="ES6中使用新的方法进行模块化： 使用 export 和 import 关键字"></a>ES6中使用新的方法进行模块化： 使用 export 和 import 关键字</h2><h3 id="方法1-声明时直接导出"><a href="#方法1-声明时直接导出" class="headerlink" title="方法1.声明时直接导出"></a>方法1.声明时直接导出</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// AAA文件中</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">var</span> a1 = <span class="string">&#x27;1&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="function"><span class="keyword">function</span> <span class="title">f1</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> f2 = <span class="function">() =&gt;</span> &#123;</span><br><span class="line"><span class="regexp">/ /</span> 箭头函数导出</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="方法2-统一在最后导出"><a href="#方法2-统一在最后导出" class="headerlink" title="方法2.统一在最后导出"></a>方法2.统一在最后导出</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// AAA文件中</span></span><br><span class="line"><span class="keyword">var</span> a1 = <span class="string">&#x27;1&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">f1</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> f2 = <span class="function">() =&gt;</span> &#123;</span><br><span class="line"><span class="regexp">/ /</span> 箭头函数导出</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> &#123;</span><br><span class="line">    a1,</span><br><span class="line">    f1,</span><br><span class="line">    f2,</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="方法3-导出时特殊操作：起别名"><a href="#方法3-导出时特殊操作：起别名" class="headerlink" title="方法3.导出时特殊操作：起别名"></a>方法3.导出时特殊操作：起别名</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// AAA文件中</span></span><br><span class="line"><span class="keyword">var</span> a1 = <span class="string">&#x27;1&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">f1</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> f2 = <span class="function">() =&gt;</span> &#123;</span><br><span class="line"><span class="regexp">/ /</span> 箭头函数导出</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// as 后跟的名字是导出的别名</span></span><br><span class="line"><span class="keyword">export</span> &#123;</span><br><span class="line">    a1 <span class="keyword">as</span> A1,</span><br><span class="line">    f1 <span class="keyword">as</span> F1,</span><br><span class="line">    f2 <span class="keyword">as</span> F2,</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="方法4-导出时特殊操作：直接导出导入内容"><a href="#方法4-导出时特殊操作：直接导出导入内容" class="headerlink" title="方法4.导出时特殊操作：直接导出导入内容"></a>方法4.导出时特殊操作：直接导出导入内容</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// 意思是导出 从XXX中导入的a1,f1,f2, 常写在一个文件中用于导出全部js，类似于库的头文件去给别人导入</span></span><br><span class="line"><span class="keyword">export</span> &#123;a1,f1,f2&#125; <span class="keyword">from</span> <span class="string">&#x27;./XXX.js&#x27;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="方法4-导出时特殊操作：使用default默认导出"><a href="#方法4-导出时特殊操作：使用default默认导出" class="headerlink" title="方法4.导出时特殊操作：使用default默认导出"></a>方法4.导出时特殊操作：使用default默认导出</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;&#125; </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>这种导出方式在导入时即可自己定义名称且不需要加{}，和其他导出方式共存，但是一个js文件只能有一个default</p>
<h3 id="方法5-导出时特殊操作：导出类"><a href="#方法5-导出时特殊操作：导出类" class="headerlink" title="方法5.导出时特殊操作：导出类"></a>方法5.导出时特殊操作：导出类</h3><p>ES6中类的本就是函数，使用时需要创建一个实例，也可以直接导出一个实例（类似于导出变量）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// AAA文件中</span></span><br><span class="line"><span class="keyword">export</span> <span class="class"><span class="keyword">class</span> <span class="title">Dog</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="title">eat</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">        <span class="built_in">console</span>.log(<span class="string">&#x27;吃饭&#x27;</span>)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// BBB 文件中</span></span><br><span class="line"><span class="keyword">import</span> &#123;Dog&#125; <span class="keyword">from</span> <span class="string">&#x27;./XXX&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> dog = <span class="keyword">new</span> Dog();</span><br><span class="line">dog.eat();</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="6-注意"><a href="#6-注意" class="headerlink" title="6.注意"></a>6.注意</h2><p>export 是关键字 ，后面跟的并不是对象，而已一个导出列表，这里的a1,f1,f2的写法并不是ES6中属性的增强写法。</p>
<h2 id="二、导入"><a href="#二、导入" class="headerlink" title="二、导入"></a>二、导入</h2><h3 id="方法1：直接导入"><a href="#方法1：直接导入" class="headerlink" title="方法1：直接导入"></a>方法1：直接导入</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123;a1,f1,f2&#125; <span class="keyword">from</span> <span class="string">&#x27;./XXX.js&#x27;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="方法2：导入时起别名"><a href="#方法2：导入时起别名" class="headerlink" title="方法2：导入时起别名"></a>方法2：导入时起别名</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123;a1 <span class="keyword">as</span> A1 ,f1 <span class="keyword">as</span> F1 ,f2 <span class="keyword">as</span> F2&#125; <span class="keyword">from</span> <span class="string">&#x27;./XXX.js&#x27;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="方法3：导入时使用"><a href="#方法3：导入时使用" class="headerlink" title="方法3：导入时使用*"></a>方法3：导入时使用<code>*</code></h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">import</span>  * <span class="keyword">as</span> aaa <span class="keyword">from</span> <span class="string">&#x27;./XXX.js&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// aaa 此时是一个对象</span></span><br><span class="line">aaa.a1;</span><br><span class="line">aaa.f1;</span><br><span class="line">aaa.f2;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="方法4：导入使用default方式导出的模块"><a href="#方法4：导入使用default方式导出的模块" class="headerlink" title="方法4：导入使用default方式导出的模块"></a>方法4：导入使用default方式导出的模块</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="comment">// aaa 此时即是导出的内容</span></span><br><span class="line"><span class="keyword">import</span>  aaa  <span class="keyword">from</span> <span class="string">&#x27;./XXX.js&#x27;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="5-注意"><a href="#5-注意" class="headerlink" title="5.注意"></a>5.注意</h3><p>import 是关键字 ，后面跟的并不是对象.<br>from 后面必须有.js 。如果是在框架中（比如Vue），则可以不写，框架会处理自动添加上。</p>
<h3 id="6-import-和-export-的本质"><a href="#6-import-和-export-的本质" class="headerlink" title="6.import 和 export 的本质"></a>6.import 和 export 的本质</h3><p>本质上是对导出对象的绑定。js引擎会有一个模块环境记录(module enviroment record) ，对所有的东西进行bindings，如果导出的js修改了导出的值，那么导入方也会被修改。这里和Node中是不同的。参考本文底部的文章。</p>
<h2 id="三、import-函数"><a href="#三、import-函数" class="headerlink" title="三、import()函数"></a>三、import()函数</h2><p>有时需要再代码中再去加载某个模块，而不是一开是就加载，这样可能会写如下代码</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">if</span>(dev)&#123;</span><br><span class="line">  <span class="keyword">import</span>(<span class="string">&#x27;./XXX.js&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>此时不能去写关键字方式的导入，因为关键字是编译时期解析，这里有js代码那么是运行时去解析的，运行时去解析编译时期解析的东西是错误的。ES6中提供了import函数去处理这样的情况。<br>需要注意的是import函数是一个异步加载的，返回的是个promise对象，所以完整的写法如下</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">if</span>(dev)&#123;</span><br><span class="line">  <span class="keyword">import</span>(<span class="string">&#x27;./XXX.js&#x27;</span>).then(<span class="function"><span class="params">res</span>=&gt;</span>&#123;</span><br><span class="line">  <span class="comment">// res 即是导出的内容</span></span><br><span class="line">  &#125;).catch(<span class="function"><span class="params">err</span>=&gt;</span>&#123;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>yinks
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://inkgn.gitee.io/2021/08/12/article8/" title="浅析ES6中的模块化">https://inkgn.gitee.io/2021/08/12/article8/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/01/article7/" rel="prev" title="ES6对象展开运算符">
      <i class="fa fa-chevron-left"></i> ES6对象展开运算符
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/08/18/git2/" rel="next" title="git笔记整理（二）">
      git笔记整理（二） <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9C%A8ES5%E4%B8%AD%E4%BD%BF%E7%94%A8commonJS-%E7%9A%84%E6%96%B9%E5%BC%8F%E8%BF%9B%E8%A1%8C%E6%A8%A1%E5%9D%97%E5%8C%96"><span class="nav-number">1.</span> <span class="nav-text">在ES5中使用commonJS 的方式进行模块化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ES6%E4%B8%AD%E4%BD%BF%E7%94%A8%E6%96%B0%E7%9A%84%E6%96%B9%E6%B3%95%E8%BF%9B%E8%A1%8C%E6%A8%A1%E5%9D%97%E5%8C%96%EF%BC%9A-%E4%BD%BF%E7%94%A8-export-%E5%92%8C-import-%E5%85%B3%E9%94%AE%E5%AD%97"><span class="nav-number">2.</span> <span class="nav-text">ES6中使用新的方法进行模块化： 使用 export 和 import 关键字</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%B9%E6%B3%951-%E5%A3%B0%E6%98%8E%E6%97%B6%E7%9B%B4%E6%8E%A5%E5%AF%BC%E5%87%BA"><span class="nav-number">2.1.</span> <span class="nav-text">方法1.声明时直接导出</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%B9%E6%B3%952-%E7%BB%9F%E4%B8%80%E5%9C%A8%E6%9C%80%E5%90%8E%E5%AF%BC%E5%87%BA"><span class="nav-number">2.2.</span> <span class="nav-text">方法2.统一在最后导出</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%B9%E6%B3%953-%E5%AF%BC%E5%87%BA%E6%97%B6%E7%89%B9%E6%AE%8A%E6%93%8D%E4%BD%9C%EF%BC%9A%E8%B5%B7%E5%88%AB%E5%90%8D"><span class="nav-number">2.3.</span> <span class="nav-text">方法3.导出时特殊操作：起别名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%B9%E6%B3%954-%E5%AF%BC%E5%87%BA%E6%97%B6%E7%89%B9%E6%AE%8A%E6%93%8D%E4%BD%9C%EF%BC%9A%E7%9B%B4%E6%8E%A5%E5%AF%BC%E5%87%BA%E5%AF%BC%E5%85%A5%E5%86%85%E5%AE%B9"><span class="nav-number">2.4.</span> <span class="nav-text">方法4.导出时特殊操作：直接导出导入内容</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%B9%E6%B3%954-%E5%AF%BC%E5%87%BA%E6%97%B6%E7%89%B9%E6%AE%8A%E6%93%8D%E4%BD%9C%EF%BC%9A%E4%BD%BF%E7%94%A8default%E9%BB%98%E8%AE%A4%E5%AF%BC%E5%87%BA"><span class="nav-number">2.5.</span> <span class="nav-text">方法4.导出时特殊操作：使用default默认导出</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%B9%E6%B3%955-%E5%AF%BC%E5%87%BA%E6%97%B6%E7%89%B9%E6%AE%8A%E6%93%8D%E4%BD%9C%EF%BC%9A%E5%AF%BC%E5%87%BA%E7%B1%BB"><span class="nav-number">2.6.</span> <span class="nav-text">方法5.导出时特殊操作：导出类</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#6-%E6%B3%A8%E6%84%8F"><span class="nav-number">3.</span> <span class="nav-text">6.注意</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%AF%BC%E5%85%A5"><span class="nav-number">4.</span> <span class="nav-text">二、导入</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%B9%E6%B3%951%EF%BC%9A%E7%9B%B4%E6%8E%A5%E5%AF%BC%E5%85%A5"><span class="nav-number">4.1.</span> <span class="nav-text">方法1：直接导入</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%B9%E6%B3%952%EF%BC%9A%E5%AF%BC%E5%85%A5%E6%97%B6%E8%B5%B7%E5%88%AB%E5%90%8D"><span class="nav-number">4.2.</span> <span class="nav-text">方法2：导入时起别名</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%B9%E6%B3%953%EF%BC%9A%E5%AF%BC%E5%85%A5%E6%97%B6%E4%BD%BF%E7%94%A8"><span class="nav-number">4.3.</span> <span class="nav-text">方法3：导入时使用*</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%B9%E6%B3%954%EF%BC%9A%E5%AF%BC%E5%85%A5%E4%BD%BF%E7%94%A8default%E6%96%B9%E5%BC%8F%E5%AF%BC%E5%87%BA%E7%9A%84%E6%A8%A1%E5%9D%97"><span class="nav-number">4.4.</span> <span class="nav-text">方法4：导入使用default方式导出的模块</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-%E6%B3%A8%E6%84%8F"><span class="nav-number">4.5.</span> <span class="nav-text">5.注意</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#6-import-%E5%92%8C-export-%E7%9A%84%E6%9C%AC%E8%B4%A8"><span class="nav-number">4.6.</span> <span class="nav-text">6.import 和 export 的本质</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%89%E3%80%81import-%E5%87%BD%E6%95%B0"><span class="nav-number">5.</span> <span class="nav-text">三、import()函数</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="yinks"
      src="https://blog-1253246719.cos.ap-chengdu.myqcloud.com/blog/user_info/face">
  <p class="site-author-name" itemprop="name">yinks</p>
  <div class="site-description" itemprop="description">长路漫漫，半程风雨，半程春</div>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://gitee.com/inkgn" title="Gitee → https:&#x2F;&#x2F;gitee.com&#x2F;inkgn" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>Gitee</a>
      </span>
      <span class="links-of-author-item">
        <a href="/2087104694@qq.com" title="E-Mail → 2087104694@qq.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">yinks</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
